package org.mclll.xwangplugin;

import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.mclll.xwangplugin.cartography.listener.CartographyListener;
import org.mclll.xwangplugin.config.GlobalConfig;
import org.mclll.xwangplugin.config.command.ReloadCommand;
import org.mclll.xwangplugin.datasource.MybatisConfig;
import org.mclll.xwangplugin.dengmi.command.DengMiGameCommand;
import org.mclll.xwangplugin.dengmi.listener.DengMiListener;
import org.mclll.xwangplugin.enchantment.command.EcmLimitCommand;
import org.mclll.xwangplugin.enchantment.config.EnchantmentLimitConfig;
import org.mclll.xwangplugin.enchantment.listener.EnchantmentListener;
import org.mclll.xwangplugin.resfly.ResFlyTimer;
import org.mclll.xwangplugin.town.listener.PlayerListener;
import org.mclll.xwangplugin.log.GlobalLog;
import org.mclll.xwangplugin.log.LogUtil;
import org.mclll.xwangplugin.log.LoggerFactory;
import org.mclll.xwangplugin.mclllFix.listener.ChatListener;
import org.mclll.xwangplugin.mcmmoCustom.poiont.command.PointReload;
import org.mclll.xwangplugin.mcmmoCustom.poiont.config.ProbBrewerConfig;
import org.mclll.xwangplugin.mcmmoCustom.poiont.listener.PointListener;
import org.mclll.xwangplugin.oraxenFix.FurniturePlaceListener;
import org.mclll.xwangplugin.plant.command.PlantCommand;
import org.mclll.xwangplugin.plant.config.PlantConfig;
import org.mclll.xwangplugin.plant.listener.PlantListener;
import org.mclll.xwangplugin.questTracking.command.QuestTrackingCommand;
import org.mclll.xwangplugin.questTracking.config.QuestTrackingConfig;
import org.mclll.xwangplugin.questTracking.gui.QuestTrackingMenuPool;
import org.mclll.xwangplugin.questTracking.listener.QuestListener;
import org.mclll.xwangplugin.questTracking.persistence.Persistence;
import org.mclll.xwangplugin.resourcePack.command.PackUploadCommand;
import org.mclll.xwangplugin.resourcePack.listener.PackListener;
import org.mclll.xwangplugin.mclllFix.listener.UseBedListener;
import org.mclll.xwangplugin.town.command.TownCommand;
import org.mclll.xwangplugin.town.config.TownContext;
import org.mclll.xwangplugin.town.shop.listener.ShopListener;
import org.mclll.xwangplugin.town.tax.TownTax;
import org.mclll.xwangplugin.util.GetChineseNameUtil;
import org.mclll.xwangplugin.web.Application;

import java.io.File;

/**
 * @author geduo
 */
@Getter
@Setter
public final class MclllCustom extends JavaPlugin {

    private LogUtil log;
    private PointListener pointListener;

    @Override
    public void onDisable() {
        // 保存任务跟踪积分板
//        Persistence.saveScoreBoard(getDataFolder());
        // 关闭Http服务
        Application.disableHttpClient();
    }

    @Override
    public void onEnable() {
        // 初始日志工具
        LoggerFactory.init(getLogger());
        this.log = LoggerFactory.get(GlobalLog.class);
        // new对象
        // 依赖mcMMO
        if (getServer().getPluginManager().getPlugin("mcMMO") != null) {
            pointListener = new PointListener(this);
        }
        // 导出配置文件
        exportConfig();
        // 注册监听器
        registerListener();
        // Timer启动
        runTimer();
        // 注册指令
        registerCommand();
        // 恢复持久化文件
        reloadData();
        // 测试
        test();
        Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
        });
        Application.startHttpClient();
        // 30分钟检查一次城镇税收
        TownTax townTax = new TownTax(this);
        Bukkit.getScheduler().runTaskTimerAsynchronously(this, townTax::taxCheck, 0, 36000L);
    }

    private void runTimer() {
        ResFlyTimer.start(this);
    }

    private void initGlobalLog(MclllCustom mclllCustom) {

    }

    private void registerCommand() {
        // 植物生长控制命令
        PluginCommand command = getCommand("poiPlant");
        command.setExecutor(new PlantCommand(this));
        // 城镇命令
        command = getCommand("poiTown");
        command.setExecutor(new TownCommand(this));
        // 附魔限制命令
        command = getCommand("ucEnchantment");
        command.setExecutor(new EcmLimitCommand(this));
        // 灯谜活动小游戏
        command = getCommand("poidengmi");
        command.setExecutor(new DengMiGameCommand(this));

        if (getServer().getPluginManager().getPlugin("Quests") != null) {
            command = getCommand("poirw");
            command.setExecutor(new QuestTrackingCommand(this));
        }

        // 依赖mcMMO
        if (getServer().getPluginManager().getPlugin("mcMMO") != null) {
            command = getCommand("poiPoint");
            command.setExecutor(new PointReload(this));
        }
        // 全局配置
        command = getCommand("poiconfig");
        command.setExecutor(new ReloadCommand(this));
        // 上传资源包
        command = getCommand("pp");
        command.setExecutor(new PackUploadCommand(this));

    }

    public void exportConfig() {
        saveResource(PlantConfig.CONFIG_FILE_NAME, false);
        saveResource(TownContext.CONFIG_FILE_NAME, false);
        saveResource(MybatisConfig.CONFIG_FILE_NAME, false);
        saveResource(GetChineseNameUtil.CONFIG_FILE_NAME, false);
        saveResource(QuestTrackingConfig.CONFIG_FILE_NAME, false);
        saveResource(ProbBrewerConfig.CONFIG_FILE_NAME, false);
        saveResource(EnchantmentLimitConfig.CONFIG_FILE_NAME, false);
        saveResource(GlobalConfig.CONFIG_FILE_NAME, false);
        TownContext.setLogger();
        TownContext.reloadConfig(getDataFolder());
        PlantConfig.reloadConfig(getDataFolder());
        EnchantmentLimitConfig.reloadConfig(getDataFolder());
        // 初始化数据库
        MybatisConfig.init(getDataFolder());
        GetChineseNameUtil.init(getDataFolder());
        // 全局配置
        GlobalConfig.init(getDataFolder());
        if (getServer().getPluginManager().getPlugin("Quests") != null) {
            QuestTrackingConfig.init(getDataFolder());
            QuestTrackingMenuPool.init(new File(getDataFolder().getParent(), "BossShopPro/shops"));
        }
        // 依赖mcMMO
        if (getServer().getPluginManager().getPlugin("mcMMO") != null) {
            ProbBrewerConfig.load(getDataFolder(), pointListener.getPotionConfig());
        }
    }

    public void reloadData() {
        // 恢复积分板文件
        Persistence.loadScoreBoard(getDataFolder());
    }

    public void registerListener() {
        PluginManager pluginManager = getServer().getPluginManager();
        pluginManager.registerEvents(new ChatListener(this), this);

        pluginManager.registerEvents(new PlantListener(this), this);
        pluginManager.registerEvents(new PlayerListener(this), this);
        if (getServer().getPluginManager().getPlugin("Quests") != null) {
            pluginManager.registerEvents(new QuestListener(this), this);
        }
        // 依赖mcMMO
        if (getServer().getPluginManager().getPlugin("mcMMO") != null) {
            pluginManager.registerEvents(pointListener, this);
        }
        pluginManager.registerEvents(new CartographyListener(this), this);
        pluginManager.registerEvents(new EnchantmentListener(this), this);
        pluginManager.registerEvents(new UseBedListener(this), this);
        pluginManager.registerEvents(new DengMiListener(), this);

        // 修复Oraxen
        if (getServer().getPluginManager().getPlugin("oraxen") != null) {
            pluginManager.registerEvents(new FurniturePlaceListener(), this);
        }

        // 城镇商店购买系统
        pluginManager.registerEvents(new ShopListener(), this);
        // itemadder材质
        if (getServer().getPluginManager().getPlugin("itemAdder") != null) {
            pluginManager.registerEvents(new PackListener(this), this);
        }
        log.info("监听器注册完毕");
    }

    private void test() {
//        GameQuestion.reload(Bukkit.getConsoleSender(), getDataFolder());
    }
}
